/**
 * 版权所有：厦门市巨龙软件工程有限公司
 * Copyright 2010 Xiamen Dragon Software Eng. Co. Ltd.
 * All right reserved. 
 *====================================================
 * 文件名称: HighScoreList.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2010-1-5			beralee(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 * 
 */
package bera.lee.memorycard;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/**
 *<pre>
 * <b><font color="blue">HighScoreList</font></b>
 * </pre>
 * 
 *<pre>
 * <b>&nbsp;--描述说明--</b>
 * </pre>
 * 
 * <pre></pre>
 * 
 * <pre>
 * <b>--样例--</b>
 *   HighScoreList obj = new HighScoreList();
 *   obj.method();
 * </pre>
 * 
 * JDK版本：JDK1.4
 * 
 * @author <b>beralee</b>
 */
public class HighScoreList extends Activity {
	private DbAdapter mDbHelper;

	private Button uploadbutton;

	private ProgressDialog dialog;

	private String calscore;

	private TextView score;

	private String paimingview;

	private String newpaiming;

	private String uploadtype;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.highscorelayout);
		mDbHelper = new DbAdapter(this);
		mDbHelper.open();
		score = (TextView) findViewById(R.id.worldscorevalue);
		uploadbutton = (Button) findViewById(R.id.buttonupload);
		Cursor wcr = mDbHelper.fetchValue("worldscore");
		String wcrvalue = wcr.getString(wcr.getColumnIndexOrThrow("value"));
		if (!wcrvalue.equals("100+")) {
			score.setText(wcrvalue);
		}
		uploadbutton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				/*
				 * Thread tt = new Thread(th); tt.start(); dialog = new
				 * ProgressDialog(HighScoreList.this);
				 * dialog.setTitle("Progress!!");
				 * dialog.setMessage("Please wait for the operation...");
				 * dialog.setCancelable(true); dialog.show();
				 */
				Cursor pointcr = mDbHelper.fetchValue("canupdate");
				String pointvalue = pointcr.getString(pointcr.getColumnIndexOrThrow("value"));
				if (pointvalue.equals("1")) {
					dialog = new ProgressDialog(HighScoreList.this);
					dialog.setTitle(HighScoreList.this.getResources().getText(R.string.progress));
					dialog.setMessage(HighScoreList.this.getResources().getText(R.string.wait));
					dialog.setCancelable(true);
					dialog.show();
					uploadtype = "upload";
					Thread thread = new Thread(th);
					thread.start();
					/*
					 * newpaiming = Upload.upload("MemoryCardFan", calscore); if
					 * (newpaiming.equals("error")) { showDialog(2); } else {
					 * score.setText(newpaiming);
					 * mDbHelper.updateValue("canupdate", "0");
					 * mDbHelper.updateValue("worldscore", newpaiming); }
					 */

				} else {
					if (Double.valueOf(calscore) < 200.000) {
						dialog = new ProgressDialog(HighScoreList.this);
						dialog.setTitle(HighScoreList.this.getResources().getText(R.string.progress));
						dialog.setMessage(HighScoreList.this.getResources().getText(R.string.wait));
						dialog.setCancelable(true);
						dialog.show();
						uploadtype = "update";
						Thread thread = new Thread(th);
						thread.start();
						/*
						 * newpaiming = Upload.update("MemoryCardFan",
						 * calscore); if (newpaiming.equals("error")) {
						 * showDialog(2); } else { score.setText(newpaiming);
						 * mDbHelper.updateValue("worldscore", newpaiming); }
						 */
					} else {
						showDialog(3);
					}

				}

			}
		});
		// 绑定Layout里面的ListView
		ListView list = (ListView) findViewById(R.id.ListView01);
	

		// 生成动态数组，加入数据
		ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		for (int i = 5; i <= 12; i++) {
			Cursor pointcr = mDbHelper.fetchValue("point" + i);
			String pointvalue = pointcr.getString(pointcr.getColumnIndexOrThrow("value"));
			Format format = new DecimalFormat("0.000");
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("point", "Level " + i);
			map.put("score", format.format((double) Double.valueOf(pointvalue) / 1000)
					+ this.getResources().getText(R.string.miao));
			listItem.add(map);
		}

		Cursor pointcr = mDbHelper.fetchValue("callent");
		String pointvalue = pointcr.getString(pointcr.getColumnIndexOrThrow("value"));
		Format format = new DecimalFormat("0.000");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("point", this.getResources().getText(R.string.CHALLENGE));
		calscore = format.format((double) Double.valueOf(pointvalue) / 1000);
		map.put("score", calscore + this.getResources().getText(R.string.miao));
		listItem.add(map);
		// 生成适配器的Item和动态数组对应的元素
		SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,// 数据源
				R.layout.list_items,// ListItem的XML实现
				// 动态数组与ImageItem对应的子项
				new String[] { "point", "score" },
				// ImageItem的XML文件里面的一个ImageView,两个TextView ID
				new int[] { R.id.point, R.id.score });

		// 添加并且显示
		list.setAdapter(listItemAdapter);

	}

	public void update() {
		score.setText(paimingview);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			dialog.dismiss();
			switch (msg.what) {
			case 0:
				if (newpaiming.equals("error")) {
					showDialog(2);
				} else {
					score.setText(newpaiming);
					mDbHelper.updateValue("canupdate", "0");
					mDbHelper.updateValue("worldscore", newpaiming);
				}
				break;
			case 1:
				if (newpaiming.equals("error")) {
					showDialog(2);
				} else {
					score.setText(newpaiming);
					mDbHelper.updateValue("worldscore", newpaiming);
				}
				break;
			}

		}
	};

	Thread th = new Thread() {

		public void run() {
			if (uploadtype.equals("upload")) {
				newpaiming = Upload.upload("Memory14", calscore);
				handler.sendEmptyMessage(0);
			} else {
				newpaiming = Upload.update("Memory14", calscore);
				handler.sendEmptyMessage(1);

			}

		}

	};

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		// 挑战模式成功后
		case 1:
			return buildDialog1(HighScoreList.this);

		case 2:
			return new AlertDialog.Builder(HighScoreList.this).setMessage(
					this.getResources().getText(R.string.neterror)).setNegativeButton("ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {

						}
					}).create();
		case 3:
			return new AlertDialog.Builder(HighScoreList.this).setMessage(
					this.getResources().getText(R.string.youmusthave)).setNegativeButton("ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {

						}
					}).create();
		case 4:
			return new AlertDialog.Builder(HighScoreList.this).setMessage(
					this.getResources().getText(R.string.youmusthave)).setNegativeButton("ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int whichButton) {

						}
					}).create();
		}
		return null;
	}

	private Dialog buildDialog1(Context context) {
		// 等待进度的对话框
		dialog = new ProgressDialog(context);
		dialog.setMessage("uploading please wait……");
		return dialog;
	}

}
